/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "wm.h"
#include "log.h"
#include "string.h"
#include "sys_task.h"
#include "app_misc.h"
#include "app_reset.h"
#include "app_battery.h"
#include "ble_app_util.h"
#include "user_protocol.h"
#include "sw_timer_task.h"
#include "app_store_mem.h"
#include "app_global_data.h"
#include "app_store_set_info.h"

/*----------------------------macro file---------------------------*/
#define SYS_TASK_LOG_EN		1

#if SYS_TASK_LOG_EN
#define SYS_TASK_LOG		LOG_DEBUG
#else
#define SYS_TASK_LOG(...)
#endif

#define SCHED_QUEUE_SIZE                    4                                /**< Maximum number of events in the scheduler queue. */
#define SCHED_MAX_EVENT_DATA_SIZE           2                                /**< Maximum size of scheduler events. */

/*----------------------------type define--------------------------*/
typedef struct
{
    uint8_t evt;
} sys_task_evt_t;

/*----------------------------var define---------------------------*/
static bool sys_task_init_done = false;
static uint32_t app_sched_buf[CEIL_DIV(APP_SCHED_BUF_SIZE(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE), sizeof(uint32_t))];

/*-------------------------func declaration------------------------*/
static void sys_task_evt_handler_scheduled_exec(void* p_event_data, uint16_t event_size);
extern void sys_ui_evt_hanlde(uint8_t evt);
#if(ROPE_CENTRAL_SUP_ENABLE)
extern void app_set_info_dev_seq_num_save(void);
extern void dev_upload_rsp_skip_dev_seq_num_burn(void);
#endif

/*-----------------------------------------------------------------*/

void sys_task_init(void)
{
	if(RET_SUCCESS != app_sched_init(SCHED_MAX_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE, app_sched_buf)) {
		app_system_reset();
	}
	sys_task_init_done = true;
	sys_task_evt_set(SYS_EVT_POWERON_FULL_DISP);
}


void sys_task_evt_set(uint8_t evt)
{
	if(false == sys_task_init_done)
		return;

	sys_task_evt_t sys_evt;
	sys_evt.evt = evt;
	
	uint32_t ret = app_sched_event_put(&sys_evt, sizeof(sys_evt), sys_task_evt_handler_scheduled_exec);
	if(RET_SUCCESS != ret) {
		LOG_WARN("ret: %d\n", ret);
	}
}


static void sys_task_evt_handler_scheduled_exec(void* p_event_data, uint16_t event_size)
{
    sys_task_evt_t const *p_sys_evt = (sys_task_evt_t *)p_event_data;

	if(p_sys_evt->evt > SYS_EVT_UI_MIN && p_sys_evt->evt < SYS_EVT_UI_MAX) {
		sys_ui_evt_hanlde(p_sys_evt->evt);
		return;
	}
	
	switch(p_sys_evt->evt)
	{
		case SYS_EVT_BATT_PER_CONVERT: {
			app_batt_adc_read_done_handle();
		} break;
		
		case SYS_EVT_STORE_MEM_ERR_HDL: {
			LOG_ERROR("SYS_EVT_STORE_MEM_ERR_HDL\n");
			app_mem_data_clear_all();
		} break;
		
		case SYS_EVT_BLE_UPLOAD_BATT_PER: {
			SYS_TASK_LOG("SYS_EVT_BLE_UPLOAD_BATT_PER\n");
			app_ble_upload_batt_per();
		} break;
		
		case SYS_EVT_BLE_UPLOAD_MEM: {
			SYS_TASK_LOG("SYS_EVT_BLE_UPLOAD_MEM\n");
			app_ble_upload_skip_result();
		} break;
		
		case SYS_EVT_SHIP_MODE_ENTER: {
			SYS_TASK_LOG("SYS_EVT_SHIP_MODE_ENTER.\n");
			app_ship_mode_enter();
		} break;
        
		
		case SYS_EVT_BLE_DEV_RESET: {
			user_upload_info_to_app_process(SKIP_CMD_DEV_RESET);
			delay_reset_timer_start(500, false);
		} break;
		
		case SYS_EVT_BLE_DEV_REVERT: {
			user_upload_info_to_app_process(SKIP_CMD_DEV_REVERT);
			delay_reset_timer_start(500, true);
		} break;
		
		case SYS_EVT_BLE_SET_ADV_NAME: {
			user_upload_info_to_app_process(SKIP_CMD_SET_ADV_NAME);
		} break;
		
		default:
			break;
	}
}

